package com.gopay.common.log.aop;

import com.gopay.common.domain.log.OperLogDtl;
import com.gopay.common.log.collect.LogBag;
import com.gopay.common.log.collect.LogCollector;
import com.gopay.common.log.constants.LogOperStatusCd;
import com.gopay.common.log.logxnew.dao.OperLogDtlManager;
import com.gopay.common.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class LogAopAdviceComponent {
	@Resource(name = "operLogDtlManager")
	private OperLogDtlManager operLogDtlManager;

    private static Logger errorlogger = Logger.getLogger(LogAopAdviceComponent.class);

    public void beforeAdvice(LogBag logBag) {
    	try{
    		LogCollector.produce(logBag);
    	}catch(Exception e){
    		errorlogger.error("beforeAdvice方法抛异常",e);
    	}
    }

    public void afterReturningAdvice(String bagId) {
    	try{
	        int elementIndex = LogCollector.getElementIndex(bagId);
	        boolean isParentHasTransactional = LogCollector.isParentHasTransactional(elementIndex);
	
	        if (!isParentHasTransactional) {
	            List<LogBag> rsList = LogCollector.popSelfAndChildrenByElementIndex(elementIndex);
	
	            LogBag logBag = null;
	            for (int i = 0; i < rsList.size(); i++) {
	                logBag = rsList.get(i);
					operLogDtlManager.saveLog(buildLogEntity(logBag));
	            }
	        }
    	}catch(Exception e){
    		errorlogger.error("afterReturningAdvice方法抛异常",e);
    	}

    }

    public void afterThrowingAdvice(String bagId, Exception e) {
    	try{
	        int elementIndex = LogCollector.getElementIndex(bagId);
	        List<LogBag> rsList = LogCollector.popSelfAndChildrenByElementIndex(elementIndex);
	
	        LogBag logBag = null;
	        for (int i = 0; i < rsList.size(); i++) {
	            logBag = rsList.get(i);
	            // 成功改成回滚
	            if (logBag.getStatus().equals(LogOperStatusCd.SUCCESS.value)) {
	                logBag.setStatus(LogOperStatusCd.ROLLBACK.value);
	            }
				operLogDtlManager.saveLog(buildLogEntity(logBag));
	        }
    	}catch(Exception e1){
    		errorlogger.error("afterThrowingAdvice方法抛异常",e1);
    	}
    }

	private OperLogDtl buildLogEntity(LogBag logBag) {
		OperLogDtl log = new OperLogDtl();
		log.setThreadId(logBag.getThreadId());
		log.setPlatform(logBag.getPlatform());
		log.setCategory(logBag.getCategory());
		log.setModules(logBag.getModules());
		log.setOperTypeCd(logBag.getOperTypeCd());
		log.setStatus(logBag.getStatus());
		log.setSnapshotBefore(logBag.getSnapshotBefore());
		log.setSnapshotAfter(logBag.getSnapshotAfter());
		log.setStepCode(logBag.getStepCode());
		log.setStepName(logBag.getStepName());
		if (StringUtils.isNotBlank(logBag.getOperTs())) {
			log.setOperTs(DateUtils.strToDate(logBag.getOperTs()));
		}
		log.setLocalIp(logBag.getLocalIp());
		log.setRemoteIp(logBag.getRemoteIp());
		log.setRemark(logBag.getRemark());
		log.setLaunchCustId(logBag.getLaunchCustId());
		log.setLaunchCustName(logBag.getLaunchCustName());
		log.setLaunchUserId(logBag.getLaunchUserId());
		log.setLaunchUserName(logBag.getLaunchUserName());
		log.setTargetCustId(logBag.getTargetCustId());
		log.setTargetCustName(logBag.getTargetCustName());
		log.setTargetUserId(logBag.getTargetUserId());
		log.setTargetUserName(logBag.getTargetUserName());
		log.setOrderId(logBag.getOrderId());
		log.setOperId(logBag.getOperId());
		if (logBag.getAssistDtl() != null) {
			log.setAssistDtl(logBag.getAssistDtl());
		}
		if (logBag.getBankRouteDtl() != null) {
			log.setBankRouteDtl(logBag.getBankRouteDtl());
		}
		log.setOperDesc(logBag.getOperDesc());
		log.setPwdCtrlAttrInfoId(logBag.getPwdCtrlAttrInfoId());
		return log;
	}

}
